package com.debai.controller;

import com.debai.pojo.Screen;
import com.debai.service.ScreenService;
import com.debai.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/screen")
@Slf4j
public class ScreenController {
    @Autowired
    private ScreenService screenService;

    //插入屏幕信息
    @PostMapping("/insert")
    public Result insert(@RequestBody Screen screen, @RequestHeader(name = "Authorization") String token) {
        String screenPosition = screen.getScreenPosition();
        if (screenService.selectByName(screenPosition) != null)
            return Result.error("该屏幕已存在");
        int i = screenService.insert(screen, token);
        if (i >= 1)
            return Result.success();

        return Result.error("插入失败");
    }

    //查看当前用户所在地区的屏幕所有信息
    @GetMapping("/selectAll")
    public Result<List<Screen>> selectAll(@RequestHeader(name = "Authorization") String token) {
        List<Screen> screens = screenService.selectAll(token);
        if (screens.size() == 0)
            return Result.error("没有数据");
        return Result.success(screens);
    }

    //对屏幕进行更新
    @PutMapping("/update")
    public Result update(@RequestBody Screen screen,@RequestHeader(name = "Authorization") String token) {
        int i = screenService.update(screen,token);
        if (i >= 1)
            return Result.success();
        return Result.error("更新失败");
    }

    //对屏幕信息进行删除
    @DeleteMapping("/delete")
    public Result delete(@RequestParam(name = "id") int id) {
        int i = screenService.delete(id);
        if (i >= 1)
            return Result.success();
        return Result.error("删除失败");
    }

}
